package 技巧;

import java.util.Arrays;

/**
 * @author TTDB
 * @create 2025/9/23
 */
public class majorityElement {
     public int majorityElement(int[] nums) {
         Arrays.sort(nums);
         int n = nums.length/2;
         return nums[n];
    }
    public int majorityElement2(int[] nums) {
        // 摩尔投票
        int x = 0, votes = 0;
        for (int num : nums){
            if (votes == 0) x = num;// x为当前众数
            votes += num == x ? 1 : -1;// votes为当前众数的票数
        }
        return x;
    }
}
